{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# from dfply import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CVaR Optimization\n",
    "## lognormal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"cvar/risk_cvar_lognormal.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "obj_coefs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][obj_coefs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][obj_coefs[0]][method[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"obj_coef\": [\"0\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Constraint\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"obj_coef\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for obj_coef in obj_coefs:\n",
    "        for m in method:\n",
    "#             df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = (risk[n][obj_coef][\"SAA\"] - risk[n][obj_coef][m])/(risk[n][obj_coef][\"SAA\"] - risk[n][obj_coef][\"oracle\"])\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = risk[n][obj_coef][m]/risk[n][obj_coef][\"oracle\"]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"grf\", \"Method\"] = \"GenRandForest\\n(modified)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"cvar/risk_lognormal.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Feature importance "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_imp = pickle.load(open(\"cvar/feature_imp_cvar_lognormal.pkl\", 'rb'))\n",
    "temp = feature_imp[\"800\"][\"0\"][0]\n",
    "mean_imp = {key: temp[key].mean(0) for key in temp.keys()}\n",
    "pd.DataFrame({key: (mean_imp[key] - np.min(mean_imp[key]))/(np.max(mean_imp[key]) - np.min(mean_imp[key])) \n",
    "              for key in mean_imp.keys()}).to_csv(\"cvar/feature_imp_cvar_lognormal.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Feature Splitting Frequency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_splt = pickle.load(open(\"cvar/feature_split_cvar_lognormal.pkl\", 'rb'))\n",
    "temp = feature_splt[\"800\"][\"0\"]\n",
    "mean_imp = {key: temp[key].mean(0) for key in temp.keys()}\n",
    "pd.DataFrame(mean_imp).to_csv(\"cvar/feature_split_cvar_lognormal.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## lognormal oracle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"cvar/risk_cvar_lognormal_oracle.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "obj_coefs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][obj_coefs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][obj_coefs[0]][method[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"obj_coef\": [\"0\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Constraint\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"obj_coef\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for obj_coef in obj_coefs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = risk[n][obj_coef][m]/risk[n][obj_coef][\"oracle\"]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"cvar/risk_lognormal_oracle.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## lognormal combined obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"cvar/risk_cvar_lognormal_objcoef.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "obj_coefs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][obj_coefs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][obj_coefs[0]][method[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    \"obj_coef\": [key for key in obj_coefs for i in range(runs * len(method))], \n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(obj_coefs),\n",
    "    \"Constraint\": [\"no\"] * len(obj_coefs) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(obj_coefs) * len(method) * runs,\n",
    "    \"n\": [\"400\"] * len(obj_coefs) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"obj_coef\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "\n",
    "for obj_coef in obj_coefs:\n",
    "    for m in method:\n",
    "        df.loc[(df.obj_coef == obj_coef) & (df.Method == m), \"rel_risk\"] = risk[\"400\"][obj_coef][m]/risk[\"400\"][obj_coef][\"oracle\"]\n",
    "\n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"cvar/risk_lognormal_objcoef.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## lognormal honesty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"cvar/risk_cvar_lognormal_honesty.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "obj_coefs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][obj_coefs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][obj_coefs[0]][method[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"obj_coef\": [\"0\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Honesty\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"obj_coef\": str,\n",
    "    \"Method\": str,\n",
    "    \"Honesty\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for obj_coef in obj_coefs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = risk[n][obj_coef][m]/risk[n][obj_coef][\"oracle\"]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_approx_sol_honest\", \"Honesty\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk_honest\", \"Honesty\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_approx_sol_dishonest\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_honest\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "\n",
    "df.loc[df.Method == \"rf_approx_risk_honest\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_dishonest\", \"Method\"] = \"StochOptForest\\n(apx-risk)\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"cvar/risk_lognormal_honesty.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## normal "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"cvar/risk_cvar_normal.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "obj_coefs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][obj_coefs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][obj_coefs[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"obj_coef\": [\"0\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Constraint\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"obj_coef\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for obj_coef in obj_coefs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = risk[n][obj_coef][m]/risk[n][obj_coef][\"oracle\"]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\"\n",
    "\n",
    "df.to_csv(\"cvar/risk_normal.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## normal oracle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"cvar/risk_cvar_normal_oracle.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "obj_coefs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][obj_coefs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][obj_coefs[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"obj_coef\": [\"0\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Constraint\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"obj_coef\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for obj_coef in obj_coefs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = risk[n][obj_coef][m]/risk[n][obj_coef][\"oracle\"]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\"\n",
    "\n",
    "df.to_csv(\"cvar/risk_normal_oracle.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Newsvendor "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sample size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'100': {'10': {'rf_approx_sol': array([15.67268561, 29.22422073, 19.15091848, 32.0937417 , 19.92149096,\n",
       "          20.6131243 , 24.55379996, 35.92681295, 23.01167747, 28.14114968,\n",
       "          19.81697063, 25.51141021, 25.46273068, 25.77754111, 30.40185463,\n",
       "          28.04470669, 34.29773415, 22.5609248 , 22.83383018, 31.97253019,\n",
       "          17.08025943, 19.88085107, 22.15605416, 27.61227754, 23.02905427,\n",
       "          34.31511062, 25.80516659, 29.64712895, 22.74029304, 20.25900445,\n",
       "          22.92498383, 22.61028886, 20.65701164, 19.38917911, 20.91791107,\n",
       "          29.53437243, 22.04684433, 34.48444512, 24.10909305, 30.59847307,\n",
       "          20.71723354, 26.46830137, 30.97888706, 18.92427199, 20.31217893,\n",
       "          26.86040384, 26.45383467, 18.72240068, 24.44854412, 22.1480747 ]),\n",
       "   'rf_approx_risk': array([17.11365223, 27.73545933, 19.41047438, 32.62003641, 19.83068211,\n",
       "          21.14421073, 23.55402199, 37.02061478, 23.34631144, 28.6568106 ,\n",
       "          22.20809934, 25.11552976, 25.93918729, 26.02918764, 30.59308489,\n",
       "          28.31213053, 33.6368421 , 23.87307931, 24.31862868, 33.24912442,\n",
       "          18.21194086, 21.40017919, 21.77838114, 28.28341303, 24.41572605,\n",
       "          33.3413828 , 24.35983905, 29.30764632, 23.88408908, 21.12573216,\n",
       "          23.78388114, 23.38725535, 21.52206185, 19.82465194, 21.2260312 ,\n",
       "          29.04889844, 21.98877972, 35.89391721, 25.50178899, 31.62047621,\n",
       "          22.8413732 , 24.96488109, 32.05498554, 19.78336751, 19.99890378,\n",
       "          27.87178119, 27.94042917, 20.2683836 , 27.18934116, 23.46151601]),\n",
       "   'rf_rf': array([17.31237327, 28.66616002, 25.77530525, 38.2659643 , 21.80053641,\n",
       "          22.72181415, 30.60687622, 40.870388  , 31.00509768, 31.58702688,\n",
       "          25.79543004, 24.02349208, 27.23093362, 24.03983795, 31.57269847,\n",
       "          32.89241254, 33.89654245, 27.02375247, 24.08316442, 38.27082213,\n",
       "          19.35764043, 24.51798352, 22.49907279, 30.04644713, 27.44442457,\n",
       "          38.80464616, 31.89329144, 34.76819297, 26.15767221, 23.78852429,\n",
       "          23.64787828, 23.07651124, 24.46526111, 21.33383808, 21.47229997,\n",
       "          28.64425205, 35.08178434, 35.68284537, 25.70846418, 31.48065477,\n",
       "          21.54596647, 31.91251908, 38.09120772, 20.22160187, 22.96419103,\n",
       "          27.32362199, 27.85700895, 19.25325494, 25.0277881 , 21.39394064]),\n",
       "   'rf_grf': array([18.0821932 , 28.62763479, 25.73369579, 37.1874316 , 21.77594826,\n",
       "          21.75523837, 29.14096445, 39.93808304, 26.89391836, 31.40822798,\n",
       "          23.99495264, 25.0529729 , 27.88093545, 26.04942905, 31.71711208,\n",
       "          32.53014654, 34.02770642, 25.62472524, 24.14542159, 34.95547134,\n",
       "          20.27744806, 23.24227713, 25.13978899, 29.3689088 , 26.18681582,\n",
       "          34.41429391, 30.67182406, 31.67871155, 24.55083454, 22.75669395,\n",
       "          24.53067437, 23.21552436, 25.3761652 , 21.00349783, 21.36923719,\n",
       "          29.66756085, 23.93579624, 37.33536351, 26.32866434, 33.23051765,\n",
       "          22.73581365, 28.16660255, 39.05465894, 20.33177994, 22.51088694,\n",
       "          27.98902336, 29.14181826, 20.50108025, 29.81522393, 24.88635893]),\n",
       "   'rf_oracle': array([15.87586372, 27.17095994, 18.77488163, 32.00735388, 17.68955126,\n",
       "          19.66902491, 23.73999182, 35.48575668, 22.85011281, 26.1456637 ,\n",
       "          19.19665152, 24.2393347 , 26.01479062, 23.08345112, 29.84739457,\n",
       "          27.8998107 , 32.6005859 , 22.62571931, 23.84664864, 31.8543472 ,\n",
       "          17.17673366, 19.88770733, 21.84297183, 27.28885454, 21.81303021,\n",
       "          32.13638155, 25.03230167, 28.85372757, 22.36922046, 20.77957212,\n",
       "          22.40399199, 23.1474031 , 19.94849484, 18.02349615, 20.71691724,\n",
       "          27.73571085, 21.75593862, 34.02219496, 22.40703869, 30.25441872,\n",
       "          22.88435758, 25.40009279, 30.47902069, 19.1137848 , 20.41970467,\n",
       "          26.95614429, 27.89796842, 18.49527676, 24.90724946, 20.6077557 ]),\n",
       "   'oracle': array([13.48607222, 16.73721832, 13.70476075, 20.16531652, 14.24101807,\n",
       "          15.69508329, 16.72611154, 15.15435443, 15.360857  , 17.0355681 ,\n",
       "          15.00110852, 16.42747298, 15.97337105, 15.27519216, 17.42490517,\n",
       "          17.58997754, 18.00594067, 13.36361606, 16.72231688, 19.49560523,\n",
       "          13.3261318 , 15.00856884, 16.16673083, 16.47527541, 16.63836497,\n",
       "          18.85880413, 13.93374193, 17.40600413, 14.39276029, 15.25357298,\n",
       "          15.31397043, 16.38393899, 14.79099556, 14.84549971, 14.97756204,\n",
       "          15.35509636, 14.98357141, 17.20831936, 14.91701756, 17.94342528,\n",
       "          15.87209352, 15.60431562, 17.1903015 , 14.10294413, 14.97878135,\n",
       "          16.3133809 , 15.45442262, 14.31521539, 14.60719181, 15.35009455])}},\n",
       " '200': {'10': {'rf_approx_sol': array([20.37665848, 26.4535136 , 16.07434446, 29.0461861 , 22.58652414,\n",
       "          18.45689746, 25.74578604, 21.39664151, 16.44316197, 22.22147086,\n",
       "          26.28731823, 22.83289506, 22.57420559, 24.82681432, 17.80687228,\n",
       "          22.36641113, 22.15333723, 18.98742787, 18.01566788, 23.11342217,\n",
       "          29.36269975, 17.24538005, 16.6228575 , 19.32134124, 21.13652554,\n",
       "          18.19139988, 17.18494147, 20.84150952, 17.69373031, 18.86120907,\n",
       "          29.7211171 , 17.92115788, 21.02833891, 28.49763808, 16.78812928,\n",
       "          19.70867524, 15.71571969, 18.41772516, 20.13996015, 19.19025661,\n",
       "          24.18647644, 17.97431558, 21.87721244, 18.77643419, 23.32967186,\n",
       "          27.501804  , 18.86253073, 23.27753772, 40.99860467, 17.45845938]),\n",
       "   'rf_approx_risk': array([21.5942472 , 27.3146936 , 16.58332977, 28.95998167, 22.75537985,\n",
       "          19.03750122, 25.85307956, 21.54597885, 17.29091282, 22.18588299,\n",
       "          27.14649119, 23.78539607, 23.96611707, 25.23359874, 18.37036577,\n",
       "          22.75578445, 24.42742456, 20.26723991, 19.15736352, 23.72199672,\n",
       "          30.42882173, 17.43851911, 16.9527408 , 19.77935375, 21.51343467,\n",
       "          17.52946203, 17.51570629, 20.61801644, 18.38479589, 19.19240218,\n",
       "          29.863063  , 18.34993126, 21.24480102, 27.80794723, 16.63992705,\n",
       "          20.20015661, 16.16792217, 18.85315697, 21.48972877, 19.78570212,\n",
       "          24.26947797, 18.1521574 , 22.27679812, 19.20127618, 23.48592677,\n",
       "          27.59660814, 18.214749  , 23.74958134, 40.97844338, 17.06797518]),\n",
       "   'rf_rf': array([27.52890393, 29.83250811, 19.16484917, 30.74382488, 25.97010106,\n",
       "          21.4982255 , 31.68866126, 25.98589241, 17.38618222, 26.79492307,\n",
       "          28.21921699, 24.24657643, 24.38856757, 25.95142117, 20.49248487,\n",
       "          25.07239984, 23.79794061, 19.14368774, 19.79535241, 23.35752196,\n",
       "          28.05563276, 21.3145478 , 18.79291229, 20.15806054, 23.27661282,\n",
       "          19.14187742, 18.0366714 , 21.22732317, 19.91445521, 20.36295256,\n",
       "          36.64292328, 19.01472554, 29.87985447, 34.36424121, 16.80194485,\n",
       "          19.39192793, 18.0807974 , 25.64128634, 20.28289878, 20.59644013,\n",
       "          27.05325949, 18.57231033, 25.17242098, 19.72684143, 24.9332483 ,\n",
       "          33.6989088 , 21.85153307, 26.0812642 , 44.93540318, 17.76665043]),\n",
       "   'rf_grf': array([24.84295899, 29.93788583, 18.69549273, 30.34802251, 24.20963181,\n",
       "          21.01655097, 27.47486056, 23.81460729, 18.16326285, 24.20436617,\n",
       "          28.76791532, 25.20083858, 26.92839943, 26.66426439, 19.88212638,\n",
       "          25.16241824, 25.22191986, 20.13454951, 21.41771842, 24.89520913,\n",
       "          30.58911872, 20.51708683, 19.29972303, 20.21312897, 23.5432134 ,\n",
       "          18.68698591, 18.68850989, 22.83177871, 19.40994614, 20.55891364,\n",
       "          38.70431755, 19.18549141, 30.21003128, 31.09339206, 18.51227744,\n",
       "          21.08391238, 18.04253278, 21.66592481, 23.77610366, 20.60131329,\n",
       "          26.35376055, 18.44597796, 28.33656518, 19.60662636, 25.03214253,\n",
       "          32.54467745, 18.98616467, 25.69605403, 45.9590389 , 17.30597416]),\n",
       "   'rf_oracle': array([20.0862182 , 26.14828915, 16.53977944, 28.92792348, 22.17354061,\n",
       "          18.18839503, 28.14602781, 21.56471412, 16.61925874, 21.86985247,\n",
       "          24.74412488, 22.39072867, 22.50696838, 24.67321844, 18.15774399,\n",
       "          22.74351454, 23.85226382, 18.79088113, 16.90264781, 23.62230194,\n",
       "          28.47569156, 17.44804165, 17.06646821, 19.5153941 , 21.42798377,\n",
       "          17.14660366, 17.89345731, 19.78110367, 18.06589459, 18.63136906,\n",
       "          29.25156597, 18.02813794, 21.2813615 , 27.80225087, 16.24576357,\n",
       "          19.55280409, 16.0258866 , 20.33670717, 19.45600873, 19.32313315,\n",
       "          23.34841945, 18.44509319, 24.40839292, 18.23731567, 23.31467662,\n",
       "          26.18428722, 18.23693486, 23.62120039, 40.17043771, 17.38931499]),\n",
       "   'oracle': array([14.89004868, 15.48683368, 13.23265923, 15.28715042, 14.98237832,\n",
       "          15.55521454, 15.50823593, 16.38793741, 13.88176136, 14.85593118,\n",
       "          17.34080393, 16.4496113 , 16.9207843 , 17.35019268, 15.28697715,\n",
       "          15.8997442 , 14.93654097, 14.70613961, 14.79044617, 17.15181754,\n",
       "          18.50470753, 14.630181  , 15.39002281, 15.77195675, 14.57799483,\n",
       "          14.98479805, 15.27849934, 16.7425945 , 14.85787733, 14.86559518,\n",
       "          15.78342066, 15.65325221, 16.01326574, 18.03315384, 14.11405867,\n",
       "          14.94803735, 12.79361781, 16.04639693, 16.01928547, 13.88576531,\n",
       "          14.51050775, 15.02705041, 17.20046584, 14.91188777, 17.06354956,\n",
       "          16.71887842, 14.78906562, 16.18528852, 19.85941101, 13.98472854])}},\n",
       " '400': {'10': {'rf_approx_sol': array([16.56069789, 18.82902013, 17.2048817 , 18.15088199, 24.17680176,\n",
       "          18.84738439, 17.00258631, 17.3538804 , 18.73522601, 21.47775621,\n",
       "          18.67495573, 17.53541807, 16.82480666, 18.99779572, 22.59158342,\n",
       "          38.06304931, 18.68228644, 15.40569926, 17.65095486, 22.87029133,\n",
       "          22.92487588, 16.00704352, 24.97695724, 18.31509056, 21.09453736,\n",
       "          16.05568764, 15.67664718, 18.62473345, 21.57595184, 20.73039679,\n",
       "          15.84739176, 34.3117711 , 17.97619614, 18.68227215, 17.3102553 ,\n",
       "          17.0526738 , 29.84973759, 21.3587592 , 19.27100777, 24.97867629,\n",
       "          18.53875514, 28.62378489, 17.08601136, 17.09546446, 16.52739578,\n",
       "          15.11415264, 20.0462166 , 19.74985225, 18.4246192 , 17.80865058]),\n",
       "   'rf_approx_risk': array([18.15422885, 18.63602373, 17.70443258, 19.04219665, 24.30338245,\n",
       "          19.65151896, 18.42517659, 17.19355444, 19.19620547, 21.41288489,\n",
       "          19.09276669, 17.16298152, 17.12856093, 18.5184069 , 22.88205067,\n",
       "          38.04954569, 18.26646231, 15.75702204, 18.17689891, 22.9551279 ,\n",
       "          22.52699718, 15.92119293, 24.37898192, 18.69791384, 21.82815216,\n",
       "          16.25732776, 16.11627952, 19.8080579 , 21.85400048, 21.51839874,\n",
       "          16.33222176, 34.18703373, 17.85675468, 18.66226257, 17.47304887,\n",
       "          17.53806573, 29.69285926, 21.04855983, 18.98698093, 25.64061502,\n",
       "          18.8830681 , 29.69829922, 17.13632851, 17.0370158 , 16.97461843,\n",
       "          15.08045742, 20.05994432, 20.83299078, 17.95164733, 17.86902898]),\n",
       "   'rf_rf': array([18.00648681, 20.70049257, 18.76776802, 20.79841688, 25.60993224,\n",
       "          19.99347581, 17.73355479, 18.05942593, 19.38495179, 22.57023848,\n",
       "          20.18417928, 18.12007071, 18.2679379 , 20.08268183, 26.20782725,\n",
       "          41.21544413, 22.10564846, 16.62423881, 22.08763579, 24.30226589,\n",
       "          22.92789314, 17.12128722, 25.5598043 , 21.6873409 , 22.26502562,\n",
       "          17.14234353, 16.54357095, 20.64218929, 22.48269655, 22.28942755,\n",
       "          16.58779324, 38.66228689, 18.46918037, 19.80417286, 19.78841678,\n",
       "          20.82853826, 32.00630294, 22.53145829, 19.72477399, 29.08647342,\n",
       "          19.6790576 , 35.67810611, 18.11340434, 18.59938355, 19.00510351,\n",
       "          16.52561322, 20.42840165, 21.85226793, 19.47162228, 18.45235355]),\n",
       "   'rf_grf': array([18.28904226, 20.12246529, 18.27100638, 19.95965659, 25.54151701,\n",
       "          20.23745791, 18.93831228, 17.66250976, 19.52393783, 22.40745596,\n",
       "          20.77465371, 18.26065607, 18.689139  , 19.35461231, 28.09665992,\n",
       "          40.40996303, 23.44325704, 17.83280582, 19.64107699, 23.82052997,\n",
       "          24.9533092 , 16.78384393, 24.70670141, 19.64232117, 22.4516934 ,\n",
       "          17.59691986, 17.03811842, 20.82042775, 22.98537159, 23.3972114 ,\n",
       "          16.95862183, 38.68319499, 19.41260817, 19.47729539, 18.98048059,\n",
       "          18.86013311, 30.77583984, 22.98251603, 19.91298209, 28.10504409,\n",
       "          20.06436992, 35.8249965 , 17.74614448, 18.11184135, 17.43174295,\n",
       "          15.87735288, 21.74898871, 22.03760854, 19.52261703, 18.88062781]),\n",
       "   'rf_oracle': array([16.70238629, 18.90418719, 17.9942219 , 18.42632118, 24.18419732,\n",
       "          19.40781339, 17.5234472 , 17.28253568, 19.23641815, 21.99928555,\n",
       "          18.9431442 , 16.78195709, 17.21546555, 19.231678  , 22.19110324,\n",
       "          38.0508863 , 18.65210809, 15.44890233, 17.85319478, 23.56297583,\n",
       "          22.27199179, 16.20858055, 25.31394744, 18.33602233, 21.08355731,\n",
       "          16.02122349, 15.92454984, 18.84874063, 21.28420382, 20.51132473,\n",
       "          15.97206575, 34.05961145, 17.65724816, 18.76341605, 17.40278732,\n",
       "          17.38015775, 29.81841385, 21.13831286, 19.18846109, 25.57573277,\n",
       "          19.36165923, 27.56255934, 17.10356166, 18.06011277, 17.26529657,\n",
       "          15.05499854, 20.18969679, 19.68902453, 18.67786375, 17.37675875]),\n",
       "   'oracle': array([15.22452076, 15.18087131, 15.31844588, 16.13288834, 16.89884153,\n",
       "          15.53414971, 14.54441929, 14.7159154 , 15.53880363, 18.89574232,\n",
       "          15.72695788, 15.50025637, 15.42515544, 14.62974092, 17.72897262,\n",
       "          19.87313094, 15.8384037 , 14.43031448, 15.52306003, 17.99052421,\n",
       "          17.42804992, 14.17635683, 16.34806402, 16.41450501, 17.18224501,\n",
       "          14.22684899, 14.40342452, 15.3766567 , 17.81908951, 16.99194707,\n",
       "          13.55403113, 17.12379036, 15.30885434, 15.85615866, 14.90101443,\n",
       "          15.08471648, 17.27622117, 15.56964645, 15.43232714, 16.53733553,\n",
       "          15.44189929, 16.9977756 , 14.99114061, 14.7051546 , 15.14461493,\n",
       "          14.02351311, 16.0330743 , 16.25485265, 15.47864465, 15.40286725])}},\n",
       " '800': {'10': {'rf_approx_sol': array([18.61295388, 15.90420958, 19.84799092, 18.11799471, 19.04557183,\n",
       "          15.69487577, 17.57382715, 19.1186681 , 16.59210686, 21.70530231,\n",
       "          23.55059767, 20.9224144 , 18.9461395 , 34.09773375, 14.92368495,\n",
       "          17.13654016, 14.4316444 , 16.76182758, 16.12253651, 15.98751532,\n",
       "          19.15390024, 22.9491031 , 15.06813522, 22.25821404, 17.58824277,\n",
       "          15.83813279, 17.94852343, 19.29817666, 21.40640126, 22.03065495,\n",
       "          27.97665483, 20.20338681, 17.59712944, 27.1685394 , 19.52168437,\n",
       "          19.53298005, 18.21262936, 17.63410983, 14.67031928, 16.66860023,\n",
       "          17.16707019, 17.38386971, 19.30061692, 18.17906378, 22.0999599 ,\n",
       "          15.5593345 , 14.77783202, 17.21771157, 22.92479206, 18.70532512]),\n",
       "   'rf_approx_risk': array([18.67823789, 16.15855124, 20.72196697, 18.23270016, 19.17085623,\n",
       "          16.0754349 , 17.85563882, 19.37941254, 16.56700543, 21.43525026,\n",
       "          23.59167553, 21.11887766, 18.74254166, 33.40634838, 14.97939012,\n",
       "          17.03714572, 14.8904821 , 17.44778084, 16.29968766, 16.28554232,\n",
       "          18.85598017, 23.02964172, 15.53841801, 21.55067516, 17.69040948,\n",
       "          15.99871891, 17.64466791, 19.47038132, 22.17499255, 21.86744394,\n",
       "          28.17976217, 20.146683  , 17.51527721, 27.93972745, 19.72571411,\n",
       "          19.82805884, 17.80533924, 17.92935223, 14.52716692, 16.88348892,\n",
       "          17.61242932, 17.28604637, 18.76867694, 18.44057362, 21.87190507,\n",
       "          16.00372551, 14.88159775, 17.11371634, 23.133283  , 18.68138009]),\n",
       "   'rf_rf': array([19.62972085, 17.00268134, 21.5331393 , 20.02721758, 19.90290848,\n",
       "          16.88457918, 19.49877354, 22.33927482, 17.95792631, 24.12726989,\n",
       "          24.10473515, 22.22981469, 20.59140179, 37.55733372, 16.01159678,\n",
       "          17.73772078, 15.33488456, 18.20054959, 17.127051  , 17.25300004,\n",
       "          21.18000134, 24.00613893, 16.06241724, 23.05442931, 18.40490184,\n",
       "          17.226589  , 18.26292066, 20.08025495, 23.78597723, 25.35250003,\n",
       "          29.28748277, 21.09980433, 18.65693879, 28.21350462, 19.95896115,\n",
       "          21.37914111, 18.5701743 , 18.62114864, 15.66226064, 17.38184581,\n",
       "          17.96416314, 20.13006779, 21.20379328, 19.7005744 , 23.03077917,\n",
       "          17.1823338 , 16.28092481, 19.47241811, 27.95706409, 19.75116601]),\n",
       "   'rf_grf': array([19.86740444, 16.93296908, 22.31527956, 20.35529109, 19.65083695,\n",
       "          16.8684319 , 19.08519564, 22.24549122, 17.38438149, 24.03197419,\n",
       "          23.9323436 , 22.7860207 , 19.8950328 , 37.65761266, 15.95663217,\n",
       "          18.52533059, 15.20520926, 18.54636349, 16.60507848, 16.66883992,\n",
       "          20.84255759, 23.86633113, 15.5665653 , 22.52550605, 17.76901623,\n",
       "          16.77168859, 18.24006885, 20.23070598, 24.97736453, 23.56269393,\n",
       "          29.62511277, 21.00050717, 18.14435906, 28.58360246, 20.55401515,\n",
       "          21.66399705, 18.69107319, 18.55464604, 15.97251693, 17.8940442 ,\n",
       "          17.99264952, 19.49984433, 19.64796621, 20.51381718, 22.45920395,\n",
       "          16.69354088, 15.58159155, 18.07709448, 25.7759612 , 19.75188589]),\n",
       "   'rf_oracle': array([18.38047395, 15.99195861, 19.24716912, 18.37450316, 19.12043411,\n",
       "          16.08278052, 17.7192215 , 18.71890186, 16.89027729, 22.26747636,\n",
       "          23.45723024, 21.56644481, 18.87324178, 34.01000245, 14.5357734 ,\n",
       "          17.37924904, 14.62051447, 17.00180047, 16.59633667, 16.13110211,\n",
       "          18.65204801, 23.17294138, 14.9486739 , 22.60870095, 18.20550719,\n",
       "          16.14680867, 17.12822298, 18.93066127, 21.75274355, 21.87849849,\n",
       "          28.01589119, 20.15587453, 17.59546931, 24.28200641, 18.2829329 ,\n",
       "          19.89407848, 18.01513396, 17.86815316, 14.55363523, 16.87850035,\n",
       "          17.05436289, 19.05580487, 19.55114762, 17.94558891, 22.25337717,\n",
       "          15.76472973, 15.19860497, 17.82371255, 24.47839141, 18.92838727]),\n",
       "   'oracle': array([15.95283606, 14.96719518, 16.00602143, 15.39963609, 17.35471447,\n",
       "          14.66152398, 16.63224983, 16.83734541, 14.91599096, 16.37670666,\n",
       "          15.95425837, 15.71403476, 16.66932037, 19.3352119 , 13.12476088,\n",
       "          14.98700833, 13.50809694, 15.82274215, 14.53439938, 15.17532991,\n",
       "          14.41326665, 18.96187589, 13.77810526, 17.70016899, 15.44880212,\n",
       "          14.18145931, 14.66450261, 16.25128219, 15.93156563, 18.21702731,\n",
       "          17.87135347, 16.65876418, 16.04484955, 18.30951911, 15.44301929,\n",
       "          14.54925702, 15.91459599, 16.48642962, 13.47736314, 15.11911231,\n",
       "          15.83476328, 15.10775758, 16.0962964 , 16.58626204, 15.71199556,\n",
       "          14.5532324 , 13.57754077, 15.88443631, 17.97193972, 16.84156183])}}}"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "risk = pickle.load(open(\"newsvendor/risk_n.pkl\", \"rb\"))\n",
    "risk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"newsvendor/risk_n.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "ps = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][ps[0]].keys())\n",
    "runs = len(risk[sample_size[0]][ps[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs,\n",
    "    \"p\": [10] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"Method\": str,\n",
    "    \"rel_risk\": float,\n",
    "    \"p\": str\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "\n",
    "for n in sample_size:\n",
    "    for p in ps:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m) & (df.p == p), \"rel_risk\"] = (risk[n][p][m])/risk[n][p][\"oracle\"]\n",
    "            \n",
    "df = df[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_grf\", \"Method\"] = \"GenRandForest\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Method</th>\n",
       "      <th>GenRandForest</th>\n",
       "      <th>RandForest</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>n</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>100</th>\n",
       "      <td>1.102898</td>\n",
       "      <td>1.115832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>200</th>\n",
       "      <td>1.117377</td>\n",
       "      <td>1.117858</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>400</th>\n",
       "      <td>1.085524</td>\n",
       "      <td>1.087415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>800</th>\n",
       "      <td>1.067499</td>\n",
       "      <td>1.076195</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Method  GenRandForest  RandForest\n",
       "n                                \n",
       "100          1.102898    1.115832\n",
       "200          1.117377    1.117858\n",
       "400          1.085524    1.087415\n",
       "800          1.067499    1.076195"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "meanrisks=df.groupby(['n','Method']).agg(mean_risk=pd.NamedAgg(column='rel_risk', aggfunc=np.mean)).reset_index().pivot(index='n', columns='Method', values='mean_risk')\n",
    "meanrisks[['GenRandForest','RandForest']].div(meanrisks['StochOptForest\\n(apx-soln)'], axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"newsvendor/risk_nv_n.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## feature splititng "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_split = pickle.load(open(\"newsvendor/feature_split_n.pkl\", \"rb\"))\n",
    "feature_split = {\n",
    "    n: {method: feature_split[n][\"10\"][method].mean(0) for method in feature_split[n][\"10\"].keys()} for n in feature_split.keys()\n",
    "}\n",
    "feature_split = feature_split[\"400\"]\n",
    "\n",
    "method = list(feature_split.keys())\n",
    "dimension = len(feature_split[method[0]])\n",
    "\n",
    "data = {\n",
    "    \"Method\": [m for m in method for i in range(dimension)],\n",
    "    \"p\": [str(d) for d in list(range(1, dimension+1))] * len(method),\n",
    "    \"freq\": [0] * len(method) * dimension\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"Method\": str,\n",
    "    \"p\": str,\n",
    "    \"freq\": float\n",
    "}\n",
    "\n",
    "df = df.astype(type_convert) \n",
    "\n",
    "for m in method:\n",
    "    for d in list(range(1, dimension + 1)):\n",
    "        df.loc[(df.Method == m) & (df.p == str(d)), \"freq\"] = feature_split[m][d-1]\n",
    "        \n",
    "df = df[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_grf\", \"Method\"] = \"GenRandForest\"\n",
    "\n",
    "df.to_csv(\"newsvendor/feature_split_nv_n.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Feature Importance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"newsvendor/feature_importance_n.pkl\", 'rb') as pickle_file:\n",
    "    fi = pickle.load(pickle_file)\n",
    "fi = fi[\"400\"][\"10\"]\n",
    "mean_imp = {key: fi[key].mean(0) for key in fi.keys()}\n",
    "pd.DataFrame({key: (mean_imp[key] - np.min(mean_imp[key]))/(np.max(mean_imp[key]) - np.min(mean_imp[key])) \n",
    "              for key in mean_imp.keys()}).to_csv(\"newsvendor/feature_importance_n.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## dimensionality "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"newsvendor/risk_p.pkl\", \"rb\"))\n",
    "\n",
    "sample_size = list(risk.keys())\n",
    "dimension = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][dimension[0]].keys())\n",
    "runs = len(risk[sample_size[0]][dimension[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"p\": [key for key in dimension for i in range(runs * len(method))],\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(dimension),\n",
    "    \"rel_risk\": [0] * len(dimension) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"p\": str,\n",
    "    \"Method\": str,\n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "\n",
    "for d in dimension:\n",
    "    for m in method:\n",
    "        df.loc[(df.p == d) & (df.Method == m), \"rel_risk\"] = risk['800'][d][m]/risk['800'][d]['oracle']\n",
    "        \n",
    "df = df[df.Method != \"oracle\"]\n",
    "df = df[df.Method != \"SAA\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_grf\", \"Method\"] = \"GenRandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\"\n",
    "df.loc[df.Method == \"knn_5\", \"Method\"] = \"5-NN\"\n",
    "df.loc[df.Method == \"knn_10\", \"Method\"] = \"10-NN\"\n",
    "df.loc[df.Method == \"knn_50\", \"Method\"] = \"50-NN\"\n",
    "\n",
    "df.to_csv(\"newsvendor/risk_nv_p.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## High Dimension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"newsvendor/risk_highdim.pkl\", \"rb\"))\n",
    "\n",
    "sample_size = list(risk.keys())\n",
    "dimension = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][dimension[0]].keys())\n",
    "runs = len(risk[sample_size[0]][dimension[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"p\": [key for key in dimension for i in range(runs * len(method))],\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(dimension),\n",
    "    \"rel_risk\": [0] * len(dimension) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"p\": str,\n",
    "    \"Method\": str,\n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "\n",
    "for d in dimension:\n",
    "    for m in method:\n",
    "        df.loc[(df.p == d) & (df.Method == m), \"rel_risk\"] = risk['200'][d][m]/risk['200'][d]['oracle']\n",
    "        \n",
    "df = df[df.Method != \"oracle\"]\n",
    "df = df[df.Method != \"SAA\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_grf\", \"Method\"] = \"GenRandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\"\n",
    "df.loc[df.Method == \"knn_5\", \"Method\"] = \"5-NN\"\n",
    "df.loc[df.Method == \"knn_10\", \"Method\"] = \"10-NN\"\n",
    "df.loc[df.Method == \"knn_50\", \"Method\"] = \"50-NN\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"newsvendor/risk_nv_highdim.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Honesty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"newsvendor/risk_nv_honesty.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "obj_coefs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][obj_coefs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][obj_coefs[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"obj_coef\": [\"0\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Honesty\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"obj_coef\": str,\n",
    "    \"Method\": str,\n",
    "    \"Honesty\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for obj_coef in obj_coefs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = risk[n][obj_coef][m]/risk[n][obj_coef][\"oracle\"]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_approx_sol_honest\", \"Honesty\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk_honest\", \"Honesty\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_approx_sol_dishonest\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_honest\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "\n",
    "df.loc[df.Method == \"rf_approx_risk_honest\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_dishonest\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "\n",
    "df.to_csv(\"newsvendor/risk_nv_honesty.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mean-variance Optimization\n",
    "\n",
    "## risk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"mean_var/rel_risk_meanvar_normal_stoch.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "Rs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][Rs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][Rs[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"R\": [\"0.5\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Constraint\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"R\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for R in Rs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = risk[n][R][m]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df = df.loc[df.Method != \"rf_oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "# df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\"\n",
    "\n",
    "df['n'] = pd.Categorical(df['n'], categories=['100', '200', '400', '800'])\n",
    "df['Method'] = pd.Categorical(df['Method'], categories=['StochOptForest\\n(apx-soln)', 'StochOptForest\\n(apx-risk)',  'RandForest', 'RandSplitForest'])\n",
    "df['Constraint'] = pd.Categorical(df['Constraint'], categories = [\"yes\", \"no\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"mean_var/rel_risk_full.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## feature splitting "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_split = pickle.load(open(\"mean_var/feature_split_meanvar_normal_stoch.pkl\", \"rb\"))\n",
    "feature_split = feature_split[\"800\"][\"0.5\"]\n",
    "feature_split = {\n",
    "    method: feature_split[method].mean(0) for method in feature_split.keys()\n",
    "}\n",
    "method = list(feature_split.keys())\n",
    "dimension = len(feature_split[method[0]])\n",
    "\n",
    "data = {\n",
    "    \"Method\": [m for m in method for i in range(dimension)],\n",
    "    \"p\": [str(d) for d in list(range(1, 11))] * len(method),\n",
    "    \"Constraint\": [\"no\"] * len(method) * dimension, \n",
    "    \"freq\": [0] * len(method) * dimension\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"Method\": str,\n",
    "    \"p\": str,\n",
    "    \"Constraint\": str,\n",
    "    \"freq\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "\n",
    "for m in method:\n",
    "    for d in list(range(1, dimension + 1)):\n",
    "        df.loc[(df.Method == m) & (df.p == str(d)), \"freq\"] = feature_split[m][d-1]\n",
    "        \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df = df.loc[df.Method != \"rf_oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "# df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"mean_var/feature_freq_full.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## conditional constraint violation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [],
   "source": [
    "violation = pickle.load(open(\"mean_var/cond_violation_meanvar_normal_stoch.pkl\", \"rb\"))\n",
    "sample_size = list(violation.keys())\n",
    "Rs = list(violation[sample_size[0]].keys())\n",
    "method = list(violation[sample_size[0]][Rs[0]].keys())\n",
    "runs = len(violation[sample_size[0]][Rs[0]][method[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"R\": [\"0.5\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Constraint\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"violation\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"R\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"violation\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for R in Rs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"violation\"] = violation[n][R][m]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df = df.loc[df.Method != \"rf_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_random_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_approx_risk_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_approx_risk_unconstr_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_approx_sol_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_approx_sol_unconstr_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_rf_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_oracle_oracle\"]\n",
    "\n",
    "# df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "# df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"mean_var/cond_violation_full.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## marginal constraint violation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": [
    "violation = pickle.load(open(\"mean_var/mean_violation_meanvar_normal_stoch.pkl\", \"rb\"))\n",
    "sample_size = list(violation.keys())\n",
    "Rs = list(violation[sample_size[0]].keys())\n",
    "method = list(violation[sample_size[0]][Rs[0]].keys())\n",
    "runs = len(violation[sample_size[0]][Rs[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"R\": [\"0.5\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Constraint\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"violation\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"R\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"violation\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for R in Rs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"violation\"] = violation[n][R][m]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df = df.loc[df.Method != \"rf_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_random_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_approx_risk_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_approx_risk_unconstr_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_approx_sol_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_approx_sol_unconstr_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_rf_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_oracle_oracle\"]\n",
    "\n",
    "# df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "# df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\"\n",
    "\n",
    "df['n'] = pd.Categorical(df['n'], categories=['100', '200', '400', \"800\"])\n",
    "df['Method'] = pd.Categorical(df['Method'], categories=['StochOptForest\\n(apx-soln)', 'StochOptForest\\n(apx-risk)', 'RandForest', 'RandSplitForest'])\n",
    "df['Constraint'] = pd.Categorical(df['Constraint'], categories = [\"yes\", \"no\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"mean_var/marginal_violation_full.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## oracle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"mean_var/rel_risk_meanvar_normal_stoch_oracle.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "Rs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][Rs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][Rs[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"R\": [\"0.1\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Constraint\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"R\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for R in Rs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = risk[n][R][m]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\"\n",
    "\n",
    "df['n'] = pd.Categorical(df['n'], categories=['100', '200', '400', '800'])\n",
    "df['Method'] = pd.Categorical(df['Method'], categories=['StochOptForest\\n(oracle)', 'StochOptForest\\n(apx-soln)', 'StochOptForest\\n(apx-risk)', 'RandForest', 'RandSplitForest'])\n",
    "df['Constraint'] = pd.Categorical(df['Constraint'], categories = [\"yes\", \"no\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"mean_var/rel_risk_full_oracle.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## oracle feature splitting "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_split = pickle.load(open(\"mean_var/feature_split_meanvar_normal_stoch_oracle.pkl\", \"rb\"))\n",
    "feature_split = feature_split[\"400\"][\"0.1\"]\n",
    "feature_split = {\n",
    "    method: feature_split[method].mean(0) for method in feature_split.keys()\n",
    "}\n",
    "method = list(feature_split.keys())\n",
    "dimension = len(feature_split[method[0]])\n",
    "\n",
    "data = {\n",
    "    \"Method\": [m for m in method for i in range(dimension)],\n",
    "    \"p\": [str(d) for d in list(range(1, 11))] * len(method),\n",
    "    \"Constraint\": [\"no\"] * len(method) * dimension, \n",
    "    \"freq\": [0] * len(method) * dimension\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"Method\": str,\n",
    "    \"p\": str,\n",
    "    \"Constraint\": str,\n",
    "    \"freq\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "\n",
    "for m in method:\n",
    "    for d in list(range(1, dimension + 1)):\n",
    "        df.loc[(df.Method == m) & (df.p == str(d)), \"freq\"] = feature_split[m][d-1]\n",
    "        \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "# df = df.loc[df.Method != \"rf_oracle\"]\n",
    "df = df.loc[df.Method != \"rf_random\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "# df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"mean_var/feature_freq_full_oracle.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## constraint threshold "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"mean_var/rel_risk_meanvar_normal_stoch_R.pkl\", \"rb\"))\n",
    "sample_size = list(risk.keys())\n",
    "Rs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][Rs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][Rs[0]][method[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    \"R\":  [key for key in Rs for i in range(runs * len(method))],\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(Rs),\n",
    "    \"Constraint\": [\"no\"] * len(Rs) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(Rs) * len(method) * runs,\n",
    "    \"n\": [\"400\"] * len(Rs) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"R\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "\n",
    "for R in Rs:\n",
    "    for m in method:\n",
    "        df.loc[(df.R == R) & (df.Method == m), \"rel_risk\"] = risk[\"400\"][R][m]          \n",
    "    \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df = df.loc[df.Method != \"rf_oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "# df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"mean_var/rel_risk_full_R.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# minimum-variance optimization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## risk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"mean_var/risk_var_normal.pkl\", \"rb\"))\n",
    "\n",
    "sample_size = list(risk.keys())\n",
    "obj_coefs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][obj_coefs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][obj_coefs[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"obj_coef\": [\"0\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Constraint\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"obj_coef\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for obj_coef in obj_coefs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = risk[n][obj_coef][m]/risk[n][obj_coef][\"oracle\"]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\"\n",
    "\n",
    "df['n'] = pd.Categorical(df['n'], categories=['100', '200', '400', '800'])\n",
    "df['Method'] = pd.Categorical(df['Method'], categories=['StochOptForest\\n(oracle)', 'StochOptForest\\n(apx-soln)', 'StochOptForest\\n(apx-risk)', 'RandForest', 'RandSplitForest'])\n",
    "df['Constraint'] = pd.Categorical(df['Constraint'], categories = [\"yes\", \"no\"])\n",
    "\n",
    "df.to_csv(\"mean_var/risk_var_normal.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## oracle "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk = pickle.load(open(\"mean_var/risk_var_normal_oracle.pkl\", \"rb\"))\n",
    "\n",
    "sample_size = list(risk.keys())\n",
    "obj_coefs = list(risk[sample_size[0]].keys())\n",
    "method = list(risk[sample_size[0]][obj_coefs[0]].keys())\n",
    "runs = len(risk[sample_size[0]][obj_coefs[0]][method[0]])\n",
    "\n",
    "data = {\n",
    "    \"n\": [key for key in sample_size for i in range(runs * len(method))],\n",
    "    \"obj_coef\": [\"0\"] * len(sample_size) * len(method) * runs,\n",
    "    \"Method\": [key for key in method for i in range(runs)] * len(sample_size),\n",
    "    \"Constraint\": [\"no\"] * len(sample_size) * len(method) * runs, \n",
    "    \"rel_risk\": [0] * len(sample_size) * len(method) * runs\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"n\": str,\n",
    "    \"obj_coef\": str,\n",
    "    \"Method\": str,\n",
    "    \"Constraint\": str, \n",
    "    \"rel_risk\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "for n in sample_size:\n",
    "    for obj_coef in obj_coefs:\n",
    "        for m in method:\n",
    "            df.loc[(df.n == n) & (df.Method == m), \"rel_risk\"] = risk[n][obj_coef][m]/risk[n][obj_coef][\"oracle\"]\n",
    "            \n",
    "df = df.loc[df.Method != \"oracle\"]\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "df.loc[df.Method == \"rf_random\", \"Method\"] = \"RandSplitForest\"\n",
    "\n",
    "df['n'] = pd.Categorical(df['n'], categories=['100', '200', '400', '800'])\n",
    "df['Method'] = pd.Categorical(df['Method'], categories=['StochOptForest\\n(oracle)', 'StochOptForest\\n(apx-soln)', 'StochOptForest\\n(apx-risk)', 'RandForest', 'RandSplitForest'])\n",
    "df['Constraint'] = pd.Categorical(df['Constraint'], categories = [\"yes\", \"no\"])\n",
    "\n",
    "df.to_csv(\"mean_var/risk_var_normal_oracle.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## oracle feature splitting "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_split = pickle.load(open(\"mean_var/feature_split_var_normal_oracle.pkl\", \"rb\"))\n",
    "feature_split = feature_split[\"400\"][\"0\"]\n",
    "feature_split = {\n",
    "    method: feature_split[method].mean(0) for method in feature_split.keys()\n",
    "}\n",
    "\n",
    "method = list(feature_split.keys())\n",
    "dimension = len(feature_split[method[0]])\n",
    "\n",
    "data = {\n",
    "    \"Method\": [m for m in method for i in range(dimension)],\n",
    "    \"p\": [str(d) for d in list(range(1, 11))] * len(method),\n",
    "    \"Constraint\": [\"no\"] * len(method) * dimension, \n",
    "    \"freq\": [0] * len(method) * dimension\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "type_convert = {\n",
    "    \"Method\": str,\n",
    "    \"p\": str,\n",
    "    \"Constraint\": str,\n",
    "    \"freq\": float\n",
    "}\n",
    "df = df.astype(type_convert) \n",
    "\n",
    "for m in method:\n",
    "    for d in list(range(1, dimension + 1)):\n",
    "        df.loc[(df.Method == m) & (df.p == str(d)), \"freq\"] = feature_split[m][d-1]\n",
    "        \n",
    "df = df.loc[df.Method != \"rf_random\"]\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Constraint\"] = \"yes\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Constraint\"] = \"yes\"\n",
    "\n",
    "df.loc[df.Method == \"rf_oracle\", \"Method\"] = \"StochOptForest\\n(oracle)\"\n",
    "df.loc[df.Method == \"rf_approx_sol\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_approx_sol_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-soln)\"\n",
    "df.loc[df.Method == \"rf_approx_risk_unconstr\", \"Method\"] = \"StochOptForest\\n(apx-risk)\"\n",
    "df.loc[df.Method == \"rf_rf\", \"Method\"] = \"RandForest\"\n",
    "\n",
    "df.to_csv(\"mean_var/feature_split_var_normal_oracle.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Uber Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [],
   "source": [
    "for year in [\"twoyear\",\"oneyear\",\"halfyear\",\"onehalfyear\"]:\n",
    "    with open(\"uber/downtown_risks_forest_years_\"+year+\".pkl\", 'rb') as pickle_file:\n",
    "        risk = pickle.load(pickle_file)\n",
    "        risk.to_csv(\"uber/downtown_risks_forest_years_\"+year+\".csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
